package org.zjx.command.basic;

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;
import org.zjx.command.Command;
import org.zjx.core.IWindow;
import org.zjx.core.impl.StartWindow;
import org.zjx.state.StateManager;
import org.zjx.util.RobotUtil;

import java.awt.*;

@Slf4j
public class MoveCommand extends Command {
    private  int x, y;

    public MoveCommand(IWindow window, int x, int y, StateManager stateManager) {
        super(window, false, stateManager);
        Rectangle bounds = window.getBounds();
        if (x < 0  || y < 0 || x > bounds.width || y > bounds.height) throw  new IllegalArgumentException("x or y out of bounds");
        this.x = x;
        this.y = y;
    }

    @Override
    protected void doExecute() throws Exception {
        if (getTargetWindow() instanceof StartWindow) {
            Rectangle bounds = getTargetWindow().getBounds();
            RobotUtil.getInstance().moveMouse(bounds.x + x, bounds.y + y);
        } else {
            User32.INSTANCE.SendMessage(getTargetWindow().getHwnd(), 0x200, new WinDef.WPARAM(0),
                    new WinDef.LPARAM(((long) y << 16) | (x & 0xFFFF)));
            if (stateManager != null) stateManager.getGameState().setGameMousePoint(new Point(x, y));
        }
    }
}